<template>
  <div v-loading="down" class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item :label="label">
          <el-date-picker v-if="isYear" v-model="formInline.date" type="monthrange" range-separator="至"
                          start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM"
          />
          <el-date-picker v-else v-model="formInline.date" type="year" value-format="yyyy" placeholder="选择年" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-close" @click="declare">
              申报
            </el-button>
          </el-form-item>
          <el-button type="primary" icon="el-icon-document-checked" :disabled="disabled" @click="downLoadPdf">
            下载PDF
          </el-button>
        </el-form-item>
      </el-form>

      <div class="result" style="margin-top: -10px;">
        <!-- <div class="tool">
          <el-button type="primary" size="mini" icon="el-icon-notebook-1" :disabled="deleteDisabled" plain
                     @click="exportData"
          />
          <el-button type="primary" size="mini" icon="el-icon-printer" plain @click="printData" />
        </div> -->
        <el-table id="table" ref="multipleTable" border :data="tableData" tooltip-effect="dark" style="width: 100%"
                  align="center" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column :prop="declareProp" :label="declareLabel" width="200" />
          <el-table-column prop="wpcName" label="产废单位名称" />
          <el-table-column label="申报时间" prop="createTime" width="200">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseDate }}
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next, sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>

    <!-- <el-dialog title="申报" :visible.sync="dialogVisible" width="25%">
      <el-form :model="form">
        <el-form-item :label="declareLabel">
          <el-date-picker
            v-if="isYear"
            v-model="form.declareMonth"
            type="month"
            placeholder="选择月"
            value-format="yyyy-MM"
          />
          <el-date-picker
            v-else
            v-model="form.declareYear"
            type="year"
            placeholder="选择年"
            value-format="yyyy"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
      </span>
    </el-dialog> -->

    <div class="dialog_diy">
      <el-dialog :visible.sync="dialogVisible" width="40%" style="margin-top: 20px">
        <div slot="title" style="background: #1890ff; color: white">
          <div v-if="isYear" style="padding: 15px 20px">废物经营月报</div>
          <div v-else style="padding: 15px 20px">废物经营年报</div>
        </div>
        <el-form :model="form" label-width="250px">
          <el-row style="margin-top: 20px">
            <el-form-item :label="declareLabel">
              <el-date-picker v-if="isYear" v-model="form.declareMonth" type="month" placeholder="选择月"
                              value-format="yyyy-MM"
              />
              <el-date-picker v-else v-model="form.declareYear" type="year" placeholder="选择年" value-format="yyyy" />
            </el-form-item>
          </el-row>

          <el-row style="margin-top: 20px; text-align: center">
            <el-form :inline="true" class="form-inline">
              <el-form-item>
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleOk">确 定</el-button>
              </el-form-item>
            </el-form>
          </el-row>
        </el-form>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import {
  pageMixin
} from "../../mixins/index";
import {
  getList,
  view,
  save
} from "../../api/declare";
import {
  saveAs
} from "file-saver";
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
      return null;
    },
  },
  mixins: [pageMixin],
  props: {
    declareType: {
      type: String,
      default: "",
    },
    label: {
      type: String,
      default: "",
    },
    name: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formInline: {
        date: "",
      },
      form: {
        declareMonth: "",
        declareYear: "",
      },
      dialogVisible: false,
      tableDetailData: [],
      down: false,
    };
  },
  computed: {
    isYear() {
      return this.label === "时间:";
    },
    declareLabel() {
      return this.label === "时间:" ? "申报月份" : "申报年份";
    },
    declareProp() {
      return this.label === "时间:" ? "declareMonth" : "declareYear";
    },
  },
  async created() {
    await this.init();
  },
  methods: {
    async downLoadPdf() {
      this.down = true;

      const result = await view(this.id, this.declareType);
      const buffer = new Blob([result]);
      const data = this.ids.find((p) => p.id === this.id);
      saveAs(buffer, `${data[this.declareProp]}_${this.name}.pdf`);
      this.down = false;
    },
    async handleOk() {
      if (!this.isYear) {
        if (!this.form.declareYear) {
          this.$message.error("请选择年份");
          return
        }
      } else {
        if (!this.form.declareMonth) {
          this.$message.error("请选择月份");
          return
        }
      }

      const {
        success,
        msg
      } = await save({
        type: this.declareType,
        DeclareMonth: this.form.declareMonth,
        DeclareYear: this.form.declareYear,
      });
      this.dialogVisible = false;
      if (success) {
        this.$notify({
          title: "成功",
          message: "申报成功",
          type: "success",
          duration: 3000,
        });
      } else {
        this.$notify.error({
          title: "失败",
          message: msg,
          duration: 3000,
        });
      }
      await this.init();
    },
    async declare() {
      this.dialogVisible = true;
    },
    async reset() {
      this.formInline = {
        date: "",
      };
      await this.init();
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      const {
        data,
        count
      } = await getList({
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        date: this.formInline.date.toString(),
        type: this.declareType,
      });
      this.tableData = data;
      this.total = count;
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
  .el-table–scrollable-y .el-table__body-wrapper {
    overflow: hidden !important;
  }

  .el-table–scrollable-x .el-table__body-wrapper {
    overflow: hidden !important;
  }

  .el-table__body-wrapper {
    overflow: hidden !important;
  }

  .result {
    margin-top: 10px;
  }

  .show-result {
    margin-top: 10px;
  }

  .abow_dialog {
    display: flex;
    justify-content: center;
    align-items: Center;
    overflow: hidden;

    .el-dialog {
      margin: 0 auto !important;
      height: 90%;
      overflow: hidden;

      .el-dialog__body {
        position: absolute;
        left: 0;
        top: 54px;
        bottom: 0;
        right: 0;
        padding: 0;
        z-index: 1;
        overflow: hidden;
        overflow-y: auto;
      }
    }
  }
</style>
